package com.byq.mm.gateway;

import java.util.LinkedList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.byq.mm.gateway.adapter.HomeAdapter;
import com.byq.mm.smartlib.component.SmartGestureListener;
import com.tc.gateway.device.Group;
import com.tc.gateway.event.GroupAddRespEvent;



public class GroupManageActivity extends Activity{
	AssistanceApplication application;
	
    GridView groupManageGrid;
    GridView sceneManageGrid;
	GestureDetector gesture;
	private ViewFlipper flipper;
    BroadcastReceiver messageReceiver;
   
    void updateAll(){
    	updateGrid(groupManageGrid);
    	updateGrid(sceneManageGrid);

    }
    void updateGrid(GridView v){
    	HomeAdapter ad = (HomeAdapter)v.getAdapter();
    	ad.reset();
    	v.setAdapter(ad);
    	
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        setContentView(R.layout.groupmanage);
        gesture = new GestureDetector(new SmartGestureListener(flipper));
        flipper = (ViewFlipper)findViewById(R.id.groupFlipper); 
        
        
        sceneManageGrid = (GridView) findViewById(R.id.sceneManageGrid);
        groupManageGrid = (GridView) findViewById(R.id.groupManageGrid);
        Button b = (Button) findViewById(R.id.createGroup);
        b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Group g = application.getHomeApplication().getGatewayEngine().getGroupManager().addGroup();
				
				updateGrid(groupManageGrid);
			}
		});
        application = (AssistanceApplication)this.getApplication();
		
       
        	groupManageGrid.setAdapter(new HomeAdapter(application, this){

				@Override
				protected boolean action(int position) {
					Object o = this.getItem(position);

					Group g = (Group)o;
					Intent intent = new Intent(); 
					
					intent.setClass(ctx,GroupEditActivity.class);
					intent.putExtra("groupId", g.getId());
			        ctx.startActivity(intent); 
			        ctx.overridePendingTransition(R.anim.zoom_enter,
			                R.anim.hold);
			        return false;

				}

				@Override
				protected void fetch() {
					devices = application.getHomeApplication().getGatewayEngine().getGroupManager().getAll();
					
				}
        		
        	});
        	sceneManageGrid.setAdapter(new HomeAdapter(application, this){

				@Override
				protected boolean action(int position) {
					// TODO Auto-generated method stub
					return false;
				}

				@Override
				protected void fetch() {
					devices =new LinkedList(application.getHomeApplication().getGatewayEngine().getSceneManager().getAll());
					
					
				}
        		
        	});

        messageReceiver = new BroadcastReceiver(){

    		@Override
    		public void onReceive(Context context, Intent intent) {
    			if(intent.getAction().equals(GroupAddRespEvent.class.getName())){
    				updateGrid(groupManageGrid);
    			}
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_UPDATE)){
    				updateAll();
    			}
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_ERROR)){
    				Toast.makeText(GroupManageActivity.this, "", Toast.LENGTH_SHORT).show();
    				updateAll();
    			}
    			
    		}
    		
    	};
       
                
                IntentFilter mf = new IntentFilter();
                mf.addAction(GroupAddRespEvent.class.getName());
        		mf.addAction(AssistanceAction.ACTION_HOME_UPDATE);
        		mf.addAction(AssistanceAction.ACTION_HOME_ERROR);
        		registerReceiver(messageReceiver, mf);	
        		
        		
        		groupManageGrid.setOnTouchListener(new View.OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						gesture.onTouchEvent(event);
						return false;
					}
				});
        		sceneManageGrid.setOnTouchListener(new View.OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						gesture.onTouchEvent(event);
						return false;
					}
				});
        		
    }
	
  

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    	boolean b = super.dispatchTouchEvent(ev);
    	if(!b)
    		gesture.onTouchEvent(ev);
    	return b;
    			
    }
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(messageReceiver == null)
			return;
		this.unregisterReceiver(messageReceiver);
		this.messageReceiver = null;
	}
	
}
